/*******************************************************************************
*
*Author         :  VN
*Description    :  网络通信模块管理类，管理通道创建和hello包等操作
*Other          :  none
*Modify Record  :  none
*
*******************************************************************************/

using System.Collections.Generic;
using System.Collections;
using ProtoBuf;
using cs;

namespace Network
{
    public class NetEngine
    {
        private uint m_uCompressSize = 0;
        private bool m_bEncrypt = true;
        private static uint m_srvtimestamp;
        private static uint m_srvSettime;

        private IDataChnl m_TCPDirChnl = null;
        private IDataChnl m_TCPGameChnl = null;

        public static uint srvtimestamp
        {
            get
            {
                uint time = m_srvtimestamp;
                System.TimeSpan ts = System.DateTime.Now - System.DateTime.Parse("1970-1-1");
                uint curTime = (uint)ts.TotalSeconds;
                time = m_srvtimestamp + (curTime - m_srvSettime);

                return time;
            }
            set
            {
                System.TimeSpan ts = System.DateTime.Now - System.DateTime.Parse("1970-1-1");
                m_srvSettime = (uint)ts.TotalSeconds;
                m_srvtimestamp = value;
            }
        }

        private static NetEngine m_NetEngine;

        static public NetEngine Instance
        {
            get
            {
                if (m_NetEngine == null)
                {
                    m_NetEngine = new NetEngine();
                }
                return m_NetEngine;
            }
        }

        public void Update()
        {
        }

        public void Initialize()
        {
            //             dataconfig.GlobalConfig gc = GameKernel.GetDataCenter().GetDataType<IResBinData>().GetGlobalConfig((uint)EnmGlobalParam.GLOBAL_COMPRESS);
            //             if (gc != null)
            //             {
            //                 m_uCompressSize = gc.GlobalValue;
            //             }
            //             m_uCompressSize = 2;
            //             gc = GameKernel.GetDataCenter().GetDataType<IResBinData>().GetGlobalConfig((uint)EnmGlobalParam.GLOBAL_ENCRYPT);
            //             if (gc != null)
            //             {
            //                 m_bEncrypt = (gc.GlobalValue == 1) ? true : false;
            //             }
        }

        public void UnInitialize()
        {
            Destroy();
        }

        public void Destroy()
        {
            DestroyDirChnl();
            DestroyGameChnl();

            m_uCompressSize = 0;
            m_bEncrypt = false;
            m_NetEngine = null;
        }

        public void DestroyDirChnl()
        {
            if (m_TCPDirChnl != null)
            {
                m_TCPDirChnl.Destroy();
                m_TCPDirChnl = null;
            }
        }

        public void DestroyGameChnl()
        {
            if (m_TCPGameChnl != null)
            {
                m_TCPGameChnl.Destroy();
                m_TCPDirChnl = null;
            }
        }

        public IDataChnl GetGameChnl()
        {
            if (m_TCPGameChnl == null)
            {
                m_TCPGameChnl = new DataChnl();
                m_TCPGameChnl.SetNetAlive(new NetAliveGameSrv());
                m_TCPGameChnl.EnabledEncrypt(m_bEncrypt);
                m_TCPGameChnl.SetCompressSize(2);
            }
            return m_TCPGameChnl;
        }

        public IDataChnl GetDirChnl()
        {
            if (m_TCPDirChnl == null)
            {
                m_TCPDirChnl = new DataChnl();
                m_TCPDirChnl.EnabledEncrypt(m_bEncrypt);
                m_TCPDirChnl.SetCompressSize(999999999);
                m_TCPDirChnl.EnabledEncrypt(m_bEncrypt);
            }
            return m_TCPDirChnl;
        }
    }
} //end namespace